package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        //定义容器保存每个月的会员数量情况
        List<Integer> memberCount = new ArrayList<Integer>();
        //遍历获取每个月的会员数量情况
        months.forEach(month->{
            Integer count = memberDao.findMemberCountBeforeDate(month);
            memberCount.add(count);
        });

        return memberCount;
    }

    @Override
    public Member findById(Integer memberId) {


        return memberDao.findById(memberId);
    }

    @Override
    public void edit(Member member) {
        memberDao.save(member);
    }
}
